package cn.pn.project.controller;

import cn.pn.project.common.FileUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;

@RestController
public class FileUP {

    //处理文件上传
    //@CrossOrigin(value = "*",maxAge = 3600,allowedHeaders = "",allowCredentials = "",methods = "*")
    @PostMapping(value="/uploadimg")
    public  String uploadImg(@RequestParam("file") MultipartFile file) {
        System.out.println(file);
        String contentType = file.getContentType();   //图片文件类型
        String fileName = file.getOriginalFilename();  //图片名字

        //文件存放路径
        String filePath = "C:\\Users\\HelloWorld\\Desktop\\";

        //调用文件处理类FileUtil，处理文件，将文件写入指定位置

        try {
            FileUtils.uploadFile(file.getBytes(), filePath, fileName);
        } catch (Exception e) {
            e.printStackTrace();
            return "{\"filepath\":\"\"}";
        }


        // 返回图片的存放路径
        return "{\"filepath\":"+filePath+"}";
    }
    @RequestMapping("/test/p/test")
    public String returnByVar1(String var1, HttpServletRequest request){
        System.out.println(request.getHeader("token"));
        return var1;
    }
}
